package com.sduept.nwld.dataserver.controller.equipment.assessment;

import com.sduept.bigdata.equipment.assessment.model.inspection.InspectionExpert;
import com.sduept.bigdata.equipment.assessment.model.problembase.ProblemBase;
import com.sduept.bigdata.equipment.assessment.service.InspectionExpertService;
import com.sduept.bigdata.equipment.assessment.service.ProblemBaseService;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.permission.entity.PersonnelEntityBean;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.event.SelectEvent;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 问题库Controller
 * @author huangyu
 * @Date 2019年9月10日
 */
@Named
@ViewScoped
public class ProblemBaseControlller extends AbstractController {

	private static final long serialVersionUID = -7657482547243113673L;
	
	@Autowired
	private ProblemBaseService service;
	@Autowired
	private InspectionExpertService expertService;
	
	private List<ProblemBase> problems;
	
	private ProblemBase selected;
	private ProblemBase current;
	
	private ProblemBase queryProblem = new ProblemBase();
	private Date queryDateStart;
	private Date queryDateEnd;
	private String expertId; 
	
	@PostConstruct
	public void init() {
		initQueryCondition();
		query();
	}

	/**
	 * 初始化查询条件
	 */
	private void initQueryCondition() {
		Date date = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
		queryDateStart = calendar.getTime();
		queryDateEnd = date;
		queryProblem = new ProblemBase();
		expertId = null;
	}
	
	public void onRowSelected(SelectEvent event) {
		selected = (ProblemBase) event.getObject();
	}
	
	/**
	 * 查询
	 */
	public void query() {
		if(StringUtils.isNotEmpty(expertId)) {
			try {
				InspectionExpert expert = expertService.findById(expertId);
				queryProblem.setCreaterCode(expert.getCode());
				queryProblem.setCreaterName(expert.getName());
			} catch (Exception e) {
				e.printStackTrace();
				addErrorMessage("获取专家失败");
			}
		}
		problems = service.findByCondition(queryDateStart, queryDateEnd, queryProblem);
	}
	/**
	 * 重置查询条件
	 */
	public void resetQuery() {
		initQueryCondition();
		query();
	}
	
	public void preCreate () {
		current = new ProblemBase();
		PersonnelEntityBean user = getCurrentUser();
		current.setCreaterCode(user.getCode());
		current.setCreaterName(user.getName());
		current.setCreateTime(new Date());
	}
	
	public void preEdit() {
		current = selected;
	}
	
	public void save() {
		service.createOrUpdate(current);
		addSuccessMessage("保存成功");
	}
	/**
	 * 删除操作
	 */
	public void delete() {
		service.delete(selected.getId());
		addSuccessMessage("删除成功");
	}
	
	public List<ProblemBase> getProblems() {
		return problems;
	}

	public void setProblems(List<ProblemBase> problems) {
		this.problems = problems;
	}

	public ProblemBase getSelected() {
		return selected;
	}

	public void setSelected(ProblemBase selected) {
		this.selected = selected;
	}

	public Date getQueryDateStart() {
		return queryDateStart;
	}

	public Date getQueryDateEnd() {
		return queryDateEnd;
	}

	public void setQueryDateStart(Date queryDateStart) {
		this.queryDateStart = queryDateStart;
	}

	public void setQueryDateEnd(Date queryDateEnd) {
		this.queryDateEnd = queryDateEnd;
	}

	public ProblemBase getCurrent() {
		return current;
	}

	public void setCurrent(ProblemBase current) {
		this.current = current;
	}

	public ProblemBase getQueryProblem() {
		return queryProblem;
	}

	public void setQueryProblem(ProblemBase queryProblem) {
		this.queryProblem = queryProblem;
	}

	public String getExpertId() {
		return expertId;
	}

	public void setExpertId(String expertId) {
		this.expertId = expertId;
	}

}
